<?php
// 应用公共文件
/**
 * 生成手机验证码
 * @param int $number 生成验证码位数
 * @return string
 * @author  yangqi
 * @time 2020年6月3日
 */
function createCode($number = 6) {
    return str_pad(mt_rand(10, 999999), $number, "0", STR_PAD_BOTH);
}

function createAppId(string $prefix = '') {
    $chars = ["a", "b", "c", "d", "e", "f",
        "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
        "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9"];
    $uuid = str_replace('-', '', \Ramsey\Uuid\Uuid::uuid1());
    $app_id = '';
    for ($i = 0; $i < 16; $i++){
        $str = substr($uuid, $i*2, 2);
        $app_id .= $chars[crc32($str) % 32];
    }
    return $app_id;
}

function createAppSecret(string $app_id) {
    // 对字符串进行排序
    $arr = str_split($app_id);
    asort($arr);
    $str = implode('', $arr);
    return md5($str . \Carbon\Carbon::now()->getPreciseTimestamp(3));
}
function get_caller_info() {
    $file = '';
    $func = '';
    $class = '';
    $trace = debug_backtrace();
    if (isset($trace[2])) {
        $file = $trace[1]['file'];
        $func = $trace[2]['function'];
        if ((substr($func, 0, 7) == 'include') || (substr($func, 0, 7) == 'require')) {
            $func = '';
        }
    } else if (isset($trace[1])) {
        $file = $trace[1]['file'];
        $func = '';
    }
    if (isset($trace[3]['class'])) {
        $class = $trace[3]['class'];
        $func = $trace[3]['function'];
        $file = $trace[2]['file'];
    } else if (isset($trace[2]['class'])) {
        $class = $trace[2]['class'];
        $func = $trace[2]['function'];
        $file = $trace[1]['file'];
    }
    return [
        'file'  => $file,
        'class' => $class,
        'function'  => $func
    ];
}

function implodeUserId(int $user1_id, int $user2_id)
{
    $ids = [$user1_id, $user2_id];
    sort($ids);
    return implode('-', $ids);
}

function explodeUserId(string $id_string)
{
    return explode('-', $id_string);
}
